"""
媒体素材数据模型
"""
from sqlalchemy import Column, String, Integer, DateTime, ForeignKey, Enum
from sqlalchemy.sql import func
from sqlalchemy.orm import relationship
from app.database import Base
import uuid
import enum


class MediaType(enum.Enum):
    """媒体类型枚举"""
    IMAGE = "image"
    VIDEO = "video"
    AUDIO = "audio"


class MediaAsset(Base):
    """媒体素材模型"""
    
    __tablename__ = "media_assets"
    
    id = Column(String(36), primary_key=True, default=lambda: str(uuid.uuid4()))
    article_id = Column(String(36), ForeignKey("articles.id", ondelete="CASCADE"), nullable=False, comment="关联文章ID")
    media_id = Column(String(100), nullable=False, comment="微信素材ID")
    original_url = Column(String(500), comment="原始图片URL")
    media_type = Column(Enum(MediaType), default=MediaType.IMAGE, comment="媒体类型")
    position = Column(Integer, comment="在文章中的位置")
    uploaded_at = Column(DateTime(timezone=True), server_default=func.now(), comment="上传时间")
    
    # 关联关系
    article = relationship("Article", back_populates="media_assets")
    
    def __repr__(self):
        return f"<MediaAsset(id={self.id}, media_id={self.media_id})>"
    
    def to_dict(self):
        """转换为字典"""
        return {
            "id": self.id,
            "article_id": self.article_id,
            "media_id": self.media_id,
            "original_url": self.original_url,
            "media_type": self.media_type.value if self.media_type else None,
            "position": self.position,
            "uploaded_at": self.uploaded_at.isoformat() if self.uploaded_at else None,
        }